package com.sl.carcode.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sl.carcode.R;
import com.sl.carcode.bean.GoodInfo;
import com.sl.carcode.global.MyApplication;

import java.util.ArrayList;


/**
 * Created by WangChang on 2016/4/1.
 */
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.BaseViewHolder> {
    private ArrayList<GoodInfo> dataList = new ArrayList<>();
    private RecyclerView recyclerView;
    private OnItemClickedListener listener;
    //初始化接口对象
    public void setOnItemClickedListener(OnItemClickedListener _listener) {
        this.listener = _listener;
    }
    public DemoAdapter(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }
    public void replaceAll(ArrayList<GoodInfo> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.one, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.setData(dataList.get(position));
    }


    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }
        void setData(Object data) {
        }
    }

    private class OneViewHolder extends BaseViewHolder {
        private ImageView ivImage;
        private final TextView tv_shapNameValue;
        public OneViewHolder(View view) {
            super(view);
            ivImage = (ImageView) view.findViewById(R.id.ivImage);
            tv_shapNameValue = view.findViewById(R.id.tv_ShapNameValue);
            int width = MyApplication.getWindowWidth();
            ViewGroup.LayoutParams params = ivImage.getLayoutParams();
            //设置图片的相对于屏幕的宽高比
            params.width = width / 2;
            params.height = (int) (200 + Math.random() * 400);
            ivImage.setLayoutParams(params);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("sl", "点击了");
                    if (listener != null) {
                        //获取点击条目的position
                        int position = recyclerView.getChildAdapterPosition(itemView);
                        Log.i("sl", "点击了" + position);
                        listener.onItemClick(position);
                    } else {
                        Log.i("sl", "点击了空");
                    }
                }
            });
        }
        @Override
        void setData(Object data) {
            if (data != null) {
                GoodInfo goodInfo = (GoodInfo) data;
                String pic = goodInfo.getPic();
                if (pic.contains("\\")) {
                    pic.replace("\\", "");
                }
                tv_shapNameValue.setText(goodInfo.getPrice());
                Glide.with(itemView.getContext()).load(pic).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.ic_launcher).crossFade().into(ivImage);
            }
        }
    }
    //已定义接口，创建抽象方法
    public interface OnItemClickedListener {
        //点击事件的方法
        void onItemClick(int position);
        //长按事件的方法
        boolean onItemLongClick(int position);
    }
}